package com.navi.loaddata.loadDao;

import com.navi.loaddata.core.errorLog.ErrorCode;
import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import com.navi.loaddata.core.oracleEntity.*;
import com.navi.loaddata.core.oracleEntity.array.EtlArrayGlassV;
import com.navi.loaddata.core.oracleEntity.array.EtlArrayIvCurveV;
import com.navi.loaddata.core.repository.impl.OracleRepositoryImpl;
import com.navi.loaddata.core.util.LoggerUtil;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xufan
 */
@Service
@Transactional(transactionManager = "thirdaryPlatformTransactionManager",
        rollbackFor = {RuntimeException.class, Exception.class})
public class SaveTeg2DataDao {
    private static final Logger logger = LoggerFactory.getLogger(SaveTeg2DataDao.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());

    @Autowired
    private OracleRepositoryImpl dao3;

    public void saveAryGlassData(EtlArrayGlassV etlArrayGlassV) throws Exception {
        Session session = dao3.getEntityManagerSession();
        String SQLFileProcedure = "{Call ARRAY_FILE_LOADER.LOAD_DATA()}";
        try {
            dao3.save(etlArrayGlassV);
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_SAVE_ORA_TEG2_ARY_GLASS, e);
        }
        // call proc
        try {
            SQLQuery queryDefect = session.createSQLQuery(SQLFileProcedure);
            queryDefect.executeUpdate();
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_PROCEDURE_ORA_ARY_GLASS, e);
        }

    }

    public void saveTeg2Data(EtlArrayIvCurveV etlArrayIvCurveV) {
        Session session = dao3.getEntityManagerSession();
        String SQLTeg2Procedure = "{Call ARRAY_FILE_LOADER.LOAD_IV_CURVE()}";
        try {
            dao3.save(etlArrayIvCurveV);
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_SAVE_ORA_TEG2_ARY_IvCurve, e);
        }


        // call proc
        try {
            SQLQuery queryTeg2 = session.createSQLQuery(SQLTeg2Procedure);
            queryTeg2.executeUpdate();
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_PROCEDURE_ORA_ARY_IvCurve, e);
        }

    }


    public void deleteTeg2() {
        try {
            Session session = dao3.getEntityManagerSession();
            String hqlGlass = "Delete FROM EtlArrayGlassV";
            String hqlTeg2 = "Delete FROM EtlArrayIvCurveV";

            Query qGlass = session.createQuery(hqlGlass);
            Query qTeg2 = session.createQuery(hqlTeg2);

            qGlass.executeUpdate();
            qTeg2.executeUpdate();
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_DELETE_ORA_TEG2, e);
        }
    }
}
